<?php 

set_time_limit(0);

switch ($_POST['type']) {
    case '1':
        echo getGJ();
        break;
    case '2':
        echo getBY_data();
    default:
        
        break;
}

function getGJ(){ //1更新硅基数据库

    //'startTime'=>'2020/6/4 00:00:00','endCallTime'=>'2020/07/4 15:05:00'

    $arrprams=['pageNo'=>1,'pageSize'=>300];

    $jsonArrPrams=json_encode($arrprams,true);

    $renwuList=json_decode(getAI('/thirdApi/bss/queryPlanCallList',$jsonArrPrams),true)['body']['list']; //获取任务列表
    $host='localhost';
    $user='root';
    $password='root';
    $database='weizandata';
    $link=mysqli_connect($host, $user, $password, $database);
    $link->query("set names utf8");
    $s=0;$e=0;$c=0;

    for($i=0;$i<count($renwuList);$i++){

        $planId=$renwuList[$i]['id'];
        $planNum=$renwuList[$i]['planNum'];
        //'fStatusList'=>[0],'startCallDur'=>30,'endCallDur'=>500
        $params=['planId'=>$planId,'pageNo'=>'1','pageSize'=>$planNum,'fStatusList'=>[0],'startCallDur'=>30,'endCallDur'=>500];

        $phoneList=json_decode(getAI('/thirdApi/bss/queryPlanPhonePage',json_encode($params,true)),true);
        
        $callId_list=$phoneList['body']['list'];

        foreach ($callId_list as $key => $value) {

            $valus.="('".implode($value,"','")."'),";

            $keys="(".implode(array_keys($value),",")."),";

            $valuA="('".implode($value,"','")."')";

            $keyA="(".implode(array_keys($value),",").")";

            $qure="insert  into GJ_datapnone {$keyA} value {$valuA}";

            //var_dump($qure);

            $result=mysqli_query($link, $qure);

            if($result){

                $s++;

            }else{

                if(mysqli_errno($link)=='1062'){

                    $c++;

                }else{

                    //$erro.= '语句：'.$qure.'<br>错误代码: '.mysqli_errno($link).'<br> 错误解释: '.mysqli_error($link).'<br>';

                    $e++;
                }
  
            }

        }

    }


     mysqli_close($link);

     $json=json_encode(['suscess'=>$s,'erro'=>$e,'chongfu'=>$c],JSON_UNESCAPED_UNICODE);

   return $json;

}

function getBY_data(){  //1

    $qure="SELECT * FROM GJ_datapnone WHERE callStartTime BETWEEN UNIX_TIMESTAMP(NOW() - INTERVAL 172 HOUR)*1000 AND UNIX_TIMESTAMP(NOW())*1000;";
    $host='localhost';
    $user='root';
    $password='root';
    $database='weizandata';
    $link=mysqli_connect($host, $user, $password, $database);
    $link->query("set names utf8");
    $result=mysqli_query($link, $qure);
    $arr=mysqli_fetch_all($result, 1);
    $jsonData=json_encode($arr);
    $json = json_encode(array(
            "code"=>0,
            "msg"=>"获取成功",
            "count"=>count($arr),
            "data"=>$arr ),
            JSON_UNESCAPED_UNICODE);
    
    mysqli_close($link);


    return $json;

}


function getAI($urli,$json){  //AI系统API

    $accessKey='MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCLZzlW68fR5x7vztskx9IvUNpsZpqfzK1QZTmQb-XH3Nn3LOGBlCMCJ_G3OQczfJnQumsqrz_vRI8c9gWhACLIwUHcHEntD3YekwxxdCR0S2EPZlwjk2t9DYHYUZQvBGHt0luclKGsP85hLCWgNMeJVKPCUPfZNwHTxBoThBwfIQIDAQAB';

    $nonce=createInvitecode();

    list($msec, $sec) = explode(' ', microtime());

    $timestamp = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);


    $secretKey='MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAItnOVbrx9HnHu_O2yTH0i9Q2mxmmp_MrVBlOZBv5cfc2fcs4YGUIwIn8bc5BzN8mdC6ayqvP-9Ejxz2BaEAIsjBQdwcSe0Pdh6TDHF0JHRLYQ9mXCOTa30NgdhRlC8EYe3SW5yUoaw_zmEsJaA0x4lUo8JQ99k3AdPEGhOEHB8hAgMBAAECgYEAidXmvCslyEXeMP83b4akLGSlk8lf2eFm_LcZ09bQPG4CloOp9ZWjh943AhNBK4doDxpEHFBrxwji-J_JsQeRW7TUZXEBZ3_JQJUO9pnB-kWNqBTcJYTrlCAwsLtJTo0KfAhj5lUupU_y-hmx6wB1mDsu-k2bsIkgSbBkzJxYxgECQQDb6RhW-Ag8ZUYFaVkNVX6Etm9nrH7k5wEURKXw1RENW15bbZnAEy2MbBTSVwvdO7b5kJhwyF7lRnVRCK7HRBqlAkEAokfY3I3YrvhRq4mDMRv_Ok8rbQiqPW5DCcfwVujL9Ods4J3t3zgsBxq0eHq-nrfYOzBIGoEm_VftvltcY7JVzQJAS2pNsY2P7X5hm_t70anPIJ4b6HbscX2TDitYqRW4qlb6LeqRPqCVD0IFhfjaFb2jy0Q-MvdUK9aU5vxOTAHcDQJAG0APVXHoSfFe3Xw48H0wDZ2PwD5tP-WNEk5luGJdJWHbMZizggFPu12ronOSINrkT4QbB4BaiL5EKOv0hrVCnQJAFkBEdSqxe11Jc-HTsEq77VzK7pz3PD6Q9xt2PsvQ5daVm3kfSdWzrfcUciqLyN9FvZPFojRX2FEOkAOLpZosyw';

    $md5=md5('accessKey='.$accessKey.'&nonce='.$nonce.'&timestamp='.$timestamp.$secretKey);

    $data= ['timestamp'=>$timestamp,'accessKey'=>$accessKey,'signType'=>'MD5','nonce'=>$nonce,'sign'=>$md5];

    //$dataPost=["pageNo"=>1,"pageSize"=> 10];

    $dataPost=json_decode($json,true);

    $dataA=array_merge($data,$dataPost);

    $url='https://bss.guiji.ai:18089'.$urli;

    return posturl($url,$dataA,$md5,$timestamp);

}



function insert_DB_GJdata($qure){  //把获取读数据插到数据库中


    $host='localhost';
    $user='root';
    $password='root';
    $database='weizandata';
    $link=mysqli_connect($host, $user, $password, $database);
    $link->query("set names utf8");
    $result=mysqli_query($link, $qure);

    $json = json_encode(array(
            "code"=>0,
            "msg"=>"",
            "count"=>count($arr),
            "data"=>$arr ),
            JSON_UNESCAPED_UNICODE);
    
    mysqli_close($link);

    return $json;

}

function  posturl($url,$params){ //

    $ch = curl_init ();

    $headers = array("Content-Type: application/json");

     $params  = json_encode($params);

    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'POST' );
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $params );
    curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
    curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSLVERSION, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $json_string = curl_exec ( $ch );
    //$data = json_decode($json_string, true);
    curl_close ( $ch );
    return $json_string;
}

function createInvitecode(){

    // 生成字母和数字组成的6位字符串

   $str = range('A', 'Z');

    // 去除大写的O，以防止与0混淆 

   unset($str[array_search('O', $str)]);

    $arr = array_merge(range(0, 9), $str);

     shuffle($arr);

    $invitecode = '';

    $arr_len = count($arr);

    for ($i = 0; $i < 6; $i++) {

        $rand = mt_rand(0, $arr_len - 1);

        $invitecode .= $arr[$rand];

    }

    return $invitecode ;

}

?>



